﻿using KAs.Application.Contract.Dto;
using KAs.Application.Contract.Dto.Interface;
using KAs.Application.Contract.Svc;
using KAs.SqlSugar.Entity;
using Microsoft.AspNetCore.Mvc;

namespace KAs.Api;

public abstract class CommonCurdController<TPrimaryKeyType, T, TCreateDto, TUpdateDto, TDto> : ControllerBase
    where T : Entity<TPrimaryKeyType>, new()
    where TCreateDto : IEntityDto<TPrimaryKeyType>, new()
    where TUpdateDto : IEntityDto<TPrimaryKeyType>, new()
    where TDto : IEntityDto<TPrimaryKeyType>, new()
{
    protected readonly ICommonCurdAppService<TPrimaryKeyType, T, TCreateDto, TUpdateDto, TDto> _appService;

    public CommonCurdController(ICommonCurdAppService<TPrimaryKeyType, T, TCreateDto, TUpdateDto, TDto> appService)
    {
        _appService = appService;
    }

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="createDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult<TPrimaryKeyType>> CreateAsync(TCreateDto createDto)
    {
        var result = ApiResult<TPrimaryKeyType>.IsSuccess();
        result.Data = await _appService.CreateAsync(createDto);
        return result;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="updateDto"></param>
    /// <returns></returns>
    [HttpPut]
    public async Task<ApiResult<bool>> UpdateAsync(TUpdateDto updateDto)
    {
        var result = ApiResult<bool>.IsSuccess();
        result.Data = await _appService.UpdateAsync(updateDto);
        return result;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<ApiResult<bool>> DeleteAsync(TPrimaryKeyType id)
    {
        var result = ApiResult<bool>.IsSuccess();
        result.Data = await _appService.DeleteAsync(id);
        return result;
    }

    /// <summary>
    /// 获取
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResult<TDto>> GetAsync(TPrimaryKeyType id)
    {
        var result = ApiResult<TDto>.IsSuccess();
        result.Data = await _appService.GetAsync(id);
        return result;
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="pageReqDto"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult<PageReturnDto<TDto>>> GetPageListAsync(PageReqDto pageReqDto)
    {
        var result = ApiResult<PageReturnDto<TDto>>.IsSuccess();
        result.Data = await _appService.GetPageListAsync(pageReqDto);
        return result;
    }
}
